package com.softstao.guoyu.ui.activity;


import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.mvp.api.VolleyBuilder;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.softstao.softstaolibrary.library.widget.EmptyLayout;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.wangjie.androidbucket.customviews.borderscrollview.OnScrollChangedListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrUIHandler;

public class BaseListActivity<T> extends BaseActivity implements OnScrollChangedListener {
    protected List<T> datas = new ArrayList<T>();
    protected RecycleViewBaseAdapter<T> adapter;
    @BindView(R.id.list)
    protected RecyclerView mRecycleView;
    @BindView(R.id.head_view)protected LinearLayout headLayout;
//    protected LinearLayout footerView;
    @BindView(R.id.more)protected View more;
    @BindView(R.id.loading) protected RelativeLayout loading;
    @BindView(R.id.scrollView)protected CustomScrollerView scrollerView;
    @BindView(R.id.empty_layout)
    protected EmptyLayout emptyLayout;
    @Override
    public int _ContentView() {
        return R.layout.activity_base_list;
    }

    @Override
    public void initView() {
        setPtrFrameLayoutEnable();
        mRecycleView.setLayoutManager(new FullyLinearLayoutManager(this));
        scrollerView.setOnScrollChangedListener(this);
    }

    //不足
    protected void setHeaderView(View header){
        getPtrFrameLayout().setHeaderView(header);
        getPtrFrameLayout().addPtrUIHandler((PtrUIHandler) header);
    }

    protected void setFooterView(View footer){
        getPtrFrameLayout().setFooterView(footer);
        getPtrFrameLayout().addPtrUIHandler((PtrUIHandler) footer);
    }
    @Override
    protected void onResume() {
        super.onResume();
        VolleyBuilder.getInstance().setIsList(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        VolleyBuilder.getInstance().setIsList(false);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        emptyLayout.setVisibility(View.GONE);
        datas.clear();
    }

    @Override
    public void onLoad() {
        super.onLoad();
        emptyLayout.setVisibility(View.GONE);
    }

    protected void isEmpty(){
        emptyLayout.setVisibility(View.VISIBLE);
    }

    @Override
    public void onScrollChanged(int i, int i1, int i2, int i3) {}
    @Override
    public void onScrollTop() {}
    @Override
    public void onScrollBottom() {
        onLoad();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(null != this.getCurrentFocus()){
            /**
             * 点击空白位置 隐藏软键盘
             */
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super .onTouchEvent(event);
    }
}
